package com.ytzl.itrip.biz.service.impl;
import com.ytzl.itrip.beans.model.vo.*;
import com.ytzl.itrip.biz.service.ItripHotelOrderService;
import com.ytzl.itrip.dao.mapper.ItripHotelOrderMapper;
import com.ytzl.itrip.beans.model.ItripHotelOrder;
import com.ytzl.itrip.utils.common.DigestUtil;
import com.ytzl.itrip.utils.common.EmptyUtils;
import com.ytzl.itrip.utils.common.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ytzl.itrip.utils.common.Constants;

@Service("itripHotelOrderService")
public class ItripHotelOrderServiceImpl implements ItripHotelOrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    public List<OrderLinkUserVo> queryLinkUserList(Long userId,String[] linkname)throws Exception{
        return itripHotelOrderMapper.queryLinkUserList(userId,linkname);
    }

    public BookBeforeOrderInfoVo getpreorderinfo(ValidateRoomStoreVo validateRoomStoreVo)throws Exception{
        BookBeforeOrderInfoVo bookBeforeOrderInfoVo =
                itripHotelOrderMapper.getpreorderinfo(validateRoomStoreVo);
        if (bookBeforeOrderInfoVo!=null){
            bookBeforeOrderInfoVo.setCount(validateRoomStoreVo.getCount());
        }
        return bookBeforeOrderInfoVo;
    }

    public ItripHotelOrderRoomVo getpersonalorderroominfo(String orderId)throws Exception{
        return itripHotelOrderMapper.getpersonalorderroominfo(orderId);
    }

    public PersonalOrderInfoVo getpersonalorderinfo(String orderId)throws Exception{
        PersonalOrderInfoVo personalOrderInfoVo =
                itripHotelOrderMapper.getpersonalorderinfo(orderId);
        if (EmptyUtils.isNotEmpty(personalOrderInfoVo)){
            Map map = new HashMap();
            map.put("1","订单提交");
            map.put("2","订单支付");
            map.put("3","支付成功");
            map.put("4","入住");
            map.put("5","订单点评");
            map.put("6","订单完成");
            personalOrderInfoVo.setOrderProcess(map);
            personalOrderInfoVo.setProcessNode(String.valueOf(map.size()));
        }
        return personalOrderInfoVo;
    }

    public ItripHotelOrderRoomVo getByOrderId(Long orderId)throws Exception{
        return itripHotelOrderMapper.getByOrderId(orderId);
    }

    public ItripHotelOrder getById(Long id)throws Exception{
        return itripHotelOrderMapper.getById(id);
    }

    public List<ItripHotelOrder> getListByMap(Map<String,Object> param)throws Exception{
        return itripHotelOrderMapper.getListByMap(param);
    }

    public Integer getCountByMap(Map<String,Object> param)throws Exception{
        return itripHotelOrderMapper.getCountByMap(param);
    }

    public Integer save(ItripHotelOrder itripHotelOrder)throws Exception{
            itripHotelOrder.setOrderNo("D1000001"
                    +new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())
                    + DigestUtil.hmacSign("123456","itrip").substring(1,6));
            itripHotelOrder.setBookingDays((int) ((itripHotelOrder.getCheckOutDate().getTime()
                                -itripHotelOrder.getCheckInDate().getTime())/(1000 * 24 * 60 * 60)));
            itripHotelOrder.setCreationDate(new Date());
            itripHotelOrder.setOrderStatus(0);
            return itripHotelOrderMapper.save(itripHotelOrder);
    }

    public Integer modify(ItripHotelOrder itripHotelOrder)throws Exception{
        itripHotelOrder.setModifyDate(new Date());
        return itripHotelOrderMapper.modify(itripHotelOrder);
    }

    public Integer removeById(Long id)throws Exception{
        return itripHotelOrderMapper.removeById(id);
    }

    public Page<List<ItripHotelOrder>> queryPageByMap(Map<String,Object> map,Integer pageNo,Integer pageSize)throws Exception{
        Integer total = itripHotelOrderMapper.getCountByMap(map);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        map.put("beginPos", page.getBeginPos());
        map.put("pageSize", page.getPageSize());
        List<ItripHotelOrder> itripHotelOrderList = itripHotelOrderMapper.getListByMap(map);
        page.setRows(itripHotelOrderList);
        return page;
    }

}
